Data Source: JHU CSSE JHU CSSE Github
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
%matplotlib inline
import seaborn as sns
import plotly.express as px
from plotly.subplots import make_subplots
sns.set(context="notebook", style="darkgrid", palette="deep", font="sans-serif", font_scale=1, color_codes=True)
%%html
<style>
div.input {
display:none;
}
</style>
from sklearn.linear_model import LinearRegression
from datetime import timedelta
import plotly.graph_objects as go
import scipy.optimize as opt
from plotly.subplots import make_subplots
from statsmodels.api import OLS
data_src="https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/"
cfm=pd.read_csv(data_src+"time_series_covid19_confirmed_global.csv")
dt=pd.read_csv(data_src+"time_series_covid19_deaths_global.csv")
##rc=pd.read_csv(data_src+"time_series_19-covid-Recovered.csv")
def trans_df(df, valname):
out=pd.DataFrame(columns=["Country/Region","Province/State","Lat","Long",
"LastUpdated",valname])
for j in df.columns[4:]:
nval=pd.notnull(df[j])
blk=df[nval][["Country/Region","Province/State","Lat","Long"]]
blk['LastUpdated']=pd.to_datetime(j)
blk[valname]=df[nval][j]
out=out.append(blk,ignore_index=True,sort=False)
out[valname]=out[valname].astype(int)
return(out)
df_cfm=trans_df(cfm,"Confirmed")
df_dt=trans_df(dt,"Death")
##df_rc=trans_df(rc,"Recovered")
df=pd.merge(df_cfm,df_dt,on=['Country/Region','Province/State','Lat','Long','LastUpdated'],how='left')
##df=pd.merge(df,df_rc,on=['Country/Region','Province/State','Lat','Long','LastUpdated'],how='left')
df.rename(columns={'Country/Region':'Country', 'Province/State':'Region','LastUpdated':'Date_updated'},inplace=True)
df.Region.fillna('',inplace=True)
df['Date_updated']=pd.to_datetime(df['Date_updated'])
#df=df[df.Date_updated <= '2020-03-28']
#fix BC data issue
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-17'), ['Confirmed','Death']]=[186,7]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-18'), ['Confirmed','Death']]=[231,7]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-19'), ['Confirmed','Death']]=[271,8]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-20'), ['Confirmed','Death']]=[348,9]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-22'), ['Confirmed','Death']]=[472,12]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-23'), ['Confirmed','Death']]=[539,12]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-25'), ['Confirmed','Death']]=[659,14]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-27'), ['Confirmed','Death']]=[792,16]
df.loc[(df.Region=="British Columbia") & (df.Date_updated=='2020-03-29'), ['Confirmed','Death']]=[900,17]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-10'), ['Confirmed','Death']]=[16,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-11'), ['Confirmed','Death']]=[24,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-12'), ['Confirmed','Death']]=[26,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-13'), ['Confirmed','Death']]=[34,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-14'), ['Confirmed','Death']]=[53,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-15'), ['Confirmed','Death']]=[63,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-16'), ['Confirmed','Death']]=[86,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-17'), ['Confirmed','Death']]=[100,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-18'), ['Confirmed','Death']]=[128,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-19'), ['Confirmed','Death']]=[155,0]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-20'), ['Confirmed','Death']]=[199,1]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-21'), ['Confirmed','Death']]=[237,1]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-22'), ['Confirmed','Death']]=[280,1]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-23'), ['Confirmed','Death']]=[331,1]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-24'), ['Confirmed','Death']]=[395,1]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-25'), ['Confirmed','Death']]=[472,2]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-26'), ['Confirmed','Death']]=[525,2]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-27'), ['Confirmed','Death']]=[608,2]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-28'), ['Confirmed','Death']]=[661,2]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-29'), ['Confirmed','Death']]=[682,2]
df.loc[(df.Region=="Alberta") & (df.Date_updated=='2020-03-30'), ['Confirmed','Death']]=[690,8]
##df[(df.Region=="British Columbia")].tail(15)
df[(df.Region=="Alberta")].tail(25)
#df[(df.Region=="Alberta")].tail(15)
##df[(df.Country=="Canada") & (df.Date_updated == '2020-03-29')].Confirmed.sum()
##df[(df.Country=="Canada") & (df.Date_updated == '2020-03-28')].sort_values('Confirmed', ascending=False)
tmp=df[['Date_updated','Country','Region','Confirmed','Death']].copy()
tmp['Date_updated_1']= tmp['Date_updated']+timedelta(days=1)
new=pd.merge(tmp,tmp,how='left',
left_on=['Country','Region','Date_updated'],
right_on=['Country','Region','Date_updated_1'],
suffixes=['_current','_before'])
new['Confirmed_added']=new['Confirmed_current']-new['Confirmed_before']
new['Death_added']=new['Death_current']-new['Death_before']
new.rename(columns={'Date_updated_current':'Date_updated'},inplace=True)
df=pd.merge(df,new[['Date_updated','Country','Region','Confirmed_added','Death_added']],
how='left',on=['Date_updated','Country','Region'])
df[(df.Country=='Canada') & (df.Date_updated=='2020-03-30')]
df_country=df.groupby(['Country','Date_updated'])[['Confirmed','Death','Confirmed_added','Death_added']].sum().reset_index()
df_now=df_country.groupby('Country')[['Confirmed','Death']].max().reset_index()
df_now[df_now.Country=='Canada']
dfa=df_country[df_country.Country.isin(df_now[df_now.Confirmed > 300]['Country'])].copy()
dfa.loc[:,'StartDate']=-999
for j in dfa.Country.unique():
sdate=dfa.loc[(dfa.Country==j) & (dfa.Confirmed > 100),'Date_updated'].min()
##print(f"Country: {j} case number: {dfa[(dfa.Country==j) & (dfa.Confirmed > 100)]['Confirmed'].min()} start date {sdate}")
tmp=dfa.loc[dfa.Country==j,'Date_updated']-sdate
dfa.loc[dfa.Country==j,'StartDate']=tmp.dt.days + 1
if j == 'China':
dfa.loc[dfa.Country==j,'StartDate']+=5
#countrylist=allcountrylist[:10]
#countrylist.append('Japan')
countrylist=[
'Spain',
'Germany',
'Iran',
'France',
'Switzerland',
'United Kingdom',
'Japan',
'Singapore']
#countrylist.remove('China').remove('Korea, South').remove('Italy')
fig=px.line(log_y=True,range_x=[0,40],range_y=[100,400000],width=1000, height=500)
#fig=px.line(dfa[dfa.Country.isin(countrylist)],x='StartDate',y='Confirmed',color='Country',log_y=True
# ,range_x=[0,40],range_y=[100,100000])
cty='China'
clr='blue'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2, color=clr))
cty='Italy'
clr='orange'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2, color=clr))
cty='US'
clr='green'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2,color=clr))
cty='Spain'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Germany'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Iran'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='France'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Korea, South'
clr='yellow'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2,color=clr))
cty='Switzerland'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='United Kingdom'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Canada'
clr='red'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines+markers",name=cty,marker=dict(size=8,color=clr),line=dict(width=4,color=clr))
cty='Japan'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Singapore'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Czechia'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Denmark'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
cty='Sweden'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'StartDate'],y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty,line=dict(width=2))
fig.update_layout(
title="COVID-19 Cases by Country",
xaxis_title="Number of Days since 100th Case",
yaxis_title="Confirmed Case",
font=dict(
family="Courier New, monospace",
size=13,
color="#7f7f7f"
)
)
fig.show()
fig.write_image("trend_by_country"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
s1_cnd=dfa.loc[(dfa.Country=='Canada') & (dfa.Confirmed > 1000),'Date_updated'].min()
fig=px.line(log_y=True,range_x=['2020-03-08','2020-06-01'],range_y=[100,100000],width=1000, height=500)
cty='China'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'Date_updated']-dfa.loc[(dfa.Country==cty) &
(dfa.Confirmed > 1000),'Date_updated'].min()+s1_cnd,y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty + "(Date Adjusted)" ,line=dict(width=4))
cty='Korea, South'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'Date_updated']-dfa.loc[(dfa.Country==cty) &
(dfa.Confirmed > 1000),'Date_updated'].min()+s1_cnd,y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty + "(Date Adjusted)",line=dict(width=4))
cty='US'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'Date_updated']-dfa.loc[(dfa.Country==cty) &
(dfa.Confirmed > 1000),'Date_updated'].min()+s1_cnd,y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty+ "(Date Adjusted)",line=dict(width=4))
cty='Italy'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'Date_updated']-dfa.loc[(dfa.Country==cty) &
(dfa.Confirmed > 1000),'Date_updated'].min()+s1_cnd,y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines",name=cty+ "(Date Adjusted)",line=dict(width=4))
cty='Canada'
fig.add_scatter(x=dfa.loc[dfa.Country==cty,'Date_updated']-dfa.loc[(dfa.Country==cty) &
(dfa.Confirmed > 1000),'Date_updated'].min()+s1_cnd,y=dfa.loc[dfa.Country==cty,'Confirmed']
,mode="lines+markers",name=cty,marker=dict(size=10))
fig.update_layout(
title="Canada COVID-19 Case",
xaxis_title="Date",
yaxis_title="Confirmed Case",
font=dict(
family="Courier New, monospace",
size=13,
color="#7f7f7f"
)
)
fig.show()
fig.write_image("canada_trend"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
pred=pd.DataFrame({'Date_updated':[ s1_cnd + timedelta(days=x-10) for x in range(100)]})
pred=pred.merge(dfa.loc[dfa.Country=='Canada',['Date_updated','Confirmed']], how='left',on='Date_updated')
dcan=dfa.loc[dfa.Country=='Canada',['Date_updated','Confirmed']].tail(5)
x=np.arange(10)
xx=np.array([np.repeat(1,10),x]).transpose()
mod=OLS(np.log(dcan['Confirmed']), xx[:5]).fit()
pdx=mod.get_prediction(xx)
predmean=np.exp(pdx.predicted_mean)
conf=np.exp(pdx.conf_int(alpha=0.05))
out=pd.DataFrame({'Date_updated': dcan['Date_updated'].append(dcan['Date_updated']+timedelta(days=5)),
'predict':predmean,
'cf_low':conf[:,0],'cf_high':conf[:,1]})
pred=pred.merge(out,how='left',on='Date_updated')
cty='US'
tmp=dfa.loc[dfa.Country==cty,['Date_updated','Confirmed']]
tmp['Date_updted_'+cty]=tmp['Date_updated']
tmp['Date_updated']=tmp['Date_updated'] - tmp.loc[tmp.Confirmed > 1000, 'Date_updated'].min() + s1_cnd
tmp.rename(columns={"Confirmed": cty + " - Date Adjusted"}, inplace=True)
pred=pred.merge(tmp,how='left', on='Date_updated')
cty='Italy'
tmp=dfa.loc[dfa.Country==cty,['Date_updated','Confirmed']]
tmp['Date_updted_'+cty]=tmp['Date_updated']
tmp['Date_updated']=tmp['Date_updated'] - tmp.loc[tmp.Confirmed > 1000, 'Date_updated'].min() + s1_cnd
tmp.rename(columns={"Confirmed": cty + " - Date Adjusted"}, inplace=True)
pred=pred.merge(tmp,how='left', on='Date_updated')
cty='Korea, South'
tmp=dfa.loc[dfa.Country==cty,['Date_updated','Confirmed']]
tmp['Date_updted_'+cty]=tmp['Date_updated']
tmp['Date_updated']=tmp['Date_updated'] - tmp.loc[tmp.Confirmed > 1000, 'Date_updated'].min() + s1_cnd
tmp.rename(columns={"Confirmed": cty + " - Date Adjusted"}, inplace=True)
pred=pred.merge(tmp,how='left', on='Date_updated')
cty='China'
tmp=dfa.loc[dfa.Country==cty,['Date_updated','Confirmed']]
tmp['Date_updted_'+cty]=tmp['Date_updated']
tmp['Date_updated']=tmp['Date_updated'] - tmp.loc[tmp.Confirmed > 1000, 'Date_updated'].min() + s1_cnd
tmp.rename(columns={"Confirmed": cty + " - Date Adjusted"}, inplace=True)
pred=pred.merge(tmp,how='left', on='Date_updated')
pred.to_csv('CanadaGrowth'+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+'.csv')
df_china=dfa[(dfa.Country=='China') & (dfa.Confirmed > 0) ]
df_italy=dfa[(dfa.Country=='Italy') & (dfa.Confirmed > 0)]
df_korea=dfa[(dfa.Country=='Korea, South') & (dfa.Confirmed > 0)]
df_us=dfa[(dfa.Country=='US') & (dfa.Confirmed > 0)]
df_canada=dfa[(dfa.Country=='Canada') & (dfa.Confirmed > 0)]
fig = go.Figure()
fig.add_trace(go.Bar(x=df_china.Date_updated,
y=df_china.Confirmed_added,
name='China',
marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(x=[pd.to_datetime('2020-01-23')], y=[30000], name='Lockdown: Jan 23', marker_color='rgb(256,0,0)'))
fig.add_trace(go.Bar(x=[pd.to_datetime('2020-03-25')], y=[30000], name='Lockdown lift: Mar 25', marker_color='rgb(256,0,0)'))
fig.add_annotation(
x=pd.to_datetime('2020-01-24'),
y=14000,
ax=100,
ay=0,
text="Lockdown: Jan 23")
fig.add_annotation(
x=pd.to_datetime('2020-02-15'),
y=5000,
ax=70,
ay=0,
text="Peak: Feb 14")
fig.add_annotation(
x=pd.to_datetime('2020-03-04'),
y=12000,
ax=0,
ay=0,
text="From lockdown to peak: 21 days",
font=dict(
color="black",
size=20
))
fig.add_annotation(
x=pd.to_datetime('2020-03-25'),
y=14000,
ax=-100,
ay=0,
text="Lockdown lifted: Mar 25")
fig.update_layout(
autosize=False,
width=800,
height=500,
margin=dict(
l=50,
r=50,
b=100,
t=100,
pad=4
),
showlegend=False,
title='China Daily Cases',
xaxis_tickfont_size=14,
xaxis=dict(
range=pd.to_datetime(['2020-01-15','2020-04-05'])),
yaxis=dict(
title='Daily Cases',
titlefont_size=16,
tickfont_size=14,
range=[0,16000]
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1, # gap between bars of the same location coordinate.
annotations=[
dict(
xref="x",
yref="y",
showarrow=True,
arrowhead=1,
arrowwidth=2
)
]
)
fig.show()
fig.write_image("china"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
fig = go.Figure()
fig.add_trace(go.Bar(x=df_korea.Date_updated,
y=df_korea.Confirmed_added,
name='Korea',
marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(x=[pd.to_datetime('2020-02-25')]
, y=[20000], name='Lockdown: Feb 25', marker_color='rgb(256,0,0)'))
fig.add_annotation(
x=pd.to_datetime('2020-02-26'),
y=900,
ax=100,
ay=0,
text="Partial Lockdown: Feb 25")
fig.add_annotation(
x=pd.to_datetime('2020-03-04'),
y=700,
ax=70,
ay=0,
text="Peak: Mar 3")
fig.add_annotation(
x=pd.to_datetime('2020-03-15'),
y=800,
ax=0,
ay=0,
text="From lockdown to peak: 7 days",
font=dict(
color="black",
size=20
))
fig.update_layout(
autosize=False,
width=800,
height=500,
margin=dict(
l=50,
r=50,
b=100,
t=100,
pad=4
),
showlegend=False,
title='Korea Daily Cases',
xaxis_tickfont_size=14,
xaxis=dict(
range=pd.to_datetime(['2020-02-16','2020-04-05'])),
yaxis=dict(
title='Daily Cases',
titlefont_size=16,
tickfont_size=14,
range=[0,1000]
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1, # gap between bars of the same location coordinate.
annotations=[
dict(
xref="x",
yref="y",
showarrow=True,
arrowhead=1,
arrowwidth=2
)
]
)
fig.show()
fig.write_image("korea"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
fig = go.Figure()
fig.add_trace(go.Bar(x=df_italy.Date_updated,
y=df_italy.Confirmed_added,
name='Italy',
marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(x=[pd.to_datetime('2020-03-09')]
, y=[20000],name='lockdown', marker_color='rgb(256,0,0)'))
fig.add_annotation(
x=pd.to_datetime('2020-03-10'),
y=7000,
ax=100,
ay=0,
text="Lockdown: Mar 09")
fig.add_annotation(
x=pd.to_datetime('2020-03-22'),
y=6500,
ax=70,
ay=0,
text="Peak: Mar 21")
fig.add_annotation(
x=pd.to_datetime('2020-02-28'),
y=5000,
ax=0,
ay=0,
text="From lockdown to peak: 12 days",
font=dict(
color="black",
size=18
))
fig.update_layout(
autosize=False,
width=800,
height=500,
margin=dict(
l=50,
r=50,
b=100,
t=100,
pad=4
),
showlegend=False,
title='Italy Daily Cases',
xaxis_tickfont_size=14,
xaxis=dict(
range=pd.to_datetime(['2020-02-20','2020-04-05'])),
yaxis=dict(
title='Daily Cases',
titlefont_size=16,
tickfont_size=14,
range=[0,9000]
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1, # gap between bars of the same location coordinate.
annotations=[
dict(
xref="x",
yref="y",
showarrow=True,
arrowhead=1,
arrowwidth=2
)
]
)
fig.show()
fig.write_image("italy"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
fig = go.Figure()
fig.add_trace(go.Bar(x=df_us.Date_updated,
y=df_us.Confirmed_added,
name='US',
marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(x=pd.to_datetime(['2020-02-29','2020-03-19'])
, y=[25000,25000],name='events', marker_color='rgb(256,0,0)'))
fig.add_annotation(
x=pd.to_datetime('2020-02-29'),
y=20000,
ax=100,
ay=0,
text="WA: State of Emergency")
fig.add_annotation(
x=pd.to_datetime('2020-03-19'),
y=15000,
ax=-150,
ay=0,
text="More states lockdown; Travel bans")
fig.update_layout(
autosize=False,
width=800,
height=500,
margin=dict(
l=50,
r=50,
b=100,
t=100,
pad=4
),
showlegend=False,
title='US Daily Cases',
xaxis_tickfont_size=14,
xaxis=dict(
range=pd.to_datetime(['2020-02-20','2020-04-05'])),
yaxis=dict(
title='Daily Cases',
titlefont_size=16,
tickfont_size=14,
range=[0,22000]
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1, # gap between bars of the same location coordinate.
annotations=[
dict(
xref="x",
yref="y",
showarrow=True,
arrowhead=1,
arrowwidth=2
)
]
)
fig.show()
fig.write_image("us"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
fig = go.Figure()
fig.add_trace(go.Bar(x=df_canada.Date_updated,
y=df_canada.Confirmed_added,
name='Canada',
marker_color='rgb(55, 83, 109)'
))
fig.add_trace(go.Bar(x=pd.to_datetime(['2020-02-26','2020-03-13','2020-03-17','2020-03-25'])
, y=[20000,20000,20000,20000],name='lockdown', marker_color='rgb(256,0,0)'))
fig.add_annotation(
x=pd.to_datetime('2020-02-26'),
y=1200,
ax=100,
ay=0,
text="Warning from Minister of Health",
font=dict( size=9))
fig.add_annotation(
x=pd.to_datetime('2020-03-13'),
y=900,
ax=-100,
ay=0,
text="Recommendation against Intl. travel",
font=dict( size=9))
fig.add_annotation(
x=pd.to_datetime('2020-03-17'),
y=1000,
ax=50,
ay=0,
text="Travel bans",
font=dict( size=9))
fig.add_annotation(
x=pd.to_datetime('2020-03-17'),
y=900,
ax=90,
ay=0,
text="State of Emergency by Prov.",
font=dict( size=9)
)
fig.add_annotation(
x=pd.to_datetime('2020-03-25'),
y=1200,
ax=-50,
ay=0,
text="Quarantine Act",
font=dict( size=9)
)
fig.update_layout(
showlegend=False,
title='Canada Daily Cases',
width=800,
height=500,
xaxis_tickfont_size=14,
xaxis=dict(
range=pd.to_datetime(['2020-02-20','2020-04-05'])),
yaxis=dict(
title='Daily Cases',
titlefont_size=16,
tickfont_size=14,
range=[0,1500]
),
legend=dict(
x=0,
y=1.0,
bgcolor='rgba(255, 255, 255, 0)',
bordercolor='rgba(255, 255, 255, 0)'
),
barmode='group',
bargap=0.15, # gap between bars of adjacent location coordinates.
bargroupgap=0.1, # gap between bars of the same location coordinate.
annotations=[
dict(
xref="x",
yref="y",
showarrow=True,
arrowhead=1,
arrowwidth=2
)
]
)
fig.show()
fig.write_image("canada"+ dfa.Date_updated.max().strftime('_%m_%d_%Y')+".jpeg")
df_mod=pd.DataFrame({'Country':dfa.Country.unique()})
df_mod['current_date']=pd.to_datetime('2020-01-01')
df_mod['current_case']=0
df_mod['slope']=0
for i, row in df_mod.iterrows():
tmp=dfa[(dfa.Country==row['Country']) & (dfa.Confirmed <=4000)].copy().tail(5)
reg=LinearRegression().fit(tmp['StartDate'][:,np.newaxis],np.log10(tmp['Confirmed']))
#df_mod['slope'].iloc[i]=reg.coef_
df_mod.loc[i,'slope']=reg.coef_
#df_mod['current_date'].iloc[i]=tmp['Date_updated'].max()
df_mod.loc[i,'current_date']=tmp['Date_updated'].max()
#df_mod['current_case'].iloc[i]=tmp['Confirmed'].max()
df_mod.loc[i,'current_case']=tmp['Confirmed'].max()
fig=px.scatter(df_mod,x='current_case',y='slope',color='Country',log_y=True, range_x=[0.01,10000])
fig.show()
df_mod[df_mod['Country']=='Germany']
fig=px.line({'Date_updated': ['2020-03-15','2020-04-02'], 'forecast': [10,10]},
x='Date_updated',y='forecast',log_y=True,
range_x=['2020-03-15','2020-03-31'],range_y=[100,500000])
clist=[
'#1f77b4', # muted blue
'#ff7f0e', # safety orange
'#2ca02c', # cooked asparagus green
'#d62728', # brick red
'#9467bd', # muted purple
'#8c564b', # chestnut brown
'#e377c2', # raspberry yogurt pink
'#7f7f7f', # middle gray
'#bcbd22', # curry yellow-green
'#17becf' # blue-teal
]
for i, ctry in enumerate(countrylist):
fig.add_scatter(x=forecast[forecast.Country==ctry]['Date_updated'],
y=forecast[forecast.Country==ctry]['forecast'],mode="lines",
line=dict(width=2,color=clist[i]),name=ctry + ' forecast')
fig.add_scatter(x=dfa[dfa.Country==ctry]['Date_updated'],
y=dfa[dfa.Country==ctry]['Confirmed'],mode="markers",
marker=dict(size=10, color=clist[i]),
name=ctry
)
fig.show()
fig=px.line({'Date_updated': ['2020-03-15','2020-04-02'], 'forecast': [10,10]},
x='Date_updated',y='forecast',
range_x=['2020-03-15','2020-03-31'],range_y=[100,150000])
clist=[
'#1f77b4', # muted blue
'#ff7f0e', # safety orange
'#2ca02c', # cooked asparagus green
'#d62728', # brick red
'#9467bd', # muted purple
'#8c564b', # chestnut brown
'#e377c2', # raspberry yogurt pink
'#7f7f7f', # middle gray
'#bcbd22', # curry yellow-green
'#17becf' # blue-teal
]
for i, ctry in enumerate(countrylist):
fig.add_scatter(x=forecast[forecast.Country==ctry]['Date_updated'],
y=forecast[forecast.Country==ctry]['forecast'],mode="lines",
line=dict(width=2,color=clist[i]),name=ctry + ' forecast')
fig.add_scatter(x=dfa[dfa.Country==ctry]['Date_updated'],
y=dfa[dfa.Country==ctry]['Confirmed'],mode="markers",
marker=dict(size=10, color=clist[i]),
name=ctry
)
fig.show()
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_x": True}]])
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[4, 5, 6],
name="yaxis1 data"
))
fig.add_trace(go.Scatter(
x=[2, 3, 4],
y=[40, 50, 60],
name="xaxis2 data",
xaxis="x2"
))
# Create axis objects
fig.update_layout(
xaxis=dict(
domain=[0.3, 0.7]
),
yaxis=dict(
title="yaxis title",
titlefont=dict(
color="#1f77b4"
),
tickfont=dict(
color="#1f77b4"
)
),
xaxis2=dict(
title="xaxis2 title",
titlefont=dict(
color="#ff7f0e"
),
tickfont=dict(
color="#ff7f0e"
),
anchor="free",
overlaying="x",
side="top",
position=1.
)
)
# Update layout properties
fig.update_layout(
title_text="multiple y-axes example",
width=800,
)
fig.show()
#countrylist=allcountrylist[:10]
#countrylist.append('Japan')
countrylist=[
'Spain',
'Germany',
'Iran',
'France',
'Switzerland',
'United Kingdom',
'Japan',
'Singapore' ]
#countrylist.remove('China').remove('Korea, South').remove('Italy')
fig = go.Figure()
fig.add_trace(go.Scatter(
x=dfa.loc[dfa.Country=='China','StartDate'],
y=dfa.loc[dfa.Country=='China','Confirmed'],
name="China",
xaxis="x1"
))
fig.add_trace(go.Scatter(
x=dfa.loc[dfa.Country=='Korea, South','StartDate'],
y=dfa.loc[dfa.Country=='Korea, South','Confirmed'],
name="Korea, South",
xaxis="x1"
))
fig.add_trace(go.Scatter(
x=dfa.loc[dfa.Country=='Korea, South','Date_updated'],
y=dfa.loc[dfa.Country=='Korea, South','Confirmed'],
name="Korea, South",
xaxis="x2"
# range_x=['2020-03-11','2020-05-09']
))
fig.update_layout(
xaxis=dict(
domain=[0.1, 0.7],
range=[1,60]
),
yaxis=dict(
title="yaxis title",
type='log',
titlefont=dict(
color="#1f77b4"
),
tickfont=dict(
color="#1f77b4"
)
),
xaxis2=dict(
range=pd.to_datetime(['2020-03-11','2020-05-09']).strftime('%m/%d/%Y'),
title="xaxis2 title",
titlefont=dict(
color="#ff7f0e"
),
tickfont=dict(
color="#ff7f0e"
),
anchor="free",
overlaying="x",
side="top",
position=1.
)
)
# Update layout properties
fig.update_layout(
title_text="multiple y-axes example",
width=1000
)
fig.show()
dfa[(dfa.Country=='Canada') & (dfa.Confirmed > 0)]
df_now.sort_values('Confirmed',ascending=False,inplace=True)
df_now[:10]
#fig=px.line(log_y=True,range_x=[0,40],range_y=[100,100000],width=1000, height=500)
#fig=px.line(dfa[dfa.Country.isin(countrylist)],x='StartDate',y='Confirmed',color='Country',log_y=True
# ,range_x=[0,40],range_y=[100,100000])
tmp=dfa[dfa.Country.isin(df_now[df_now.Confirmed > 2000]['Country'])]
px.line(tmp,x='StartDate',y='Confirmed',color='Country',log_y=False, range_x=[0,20], range_y=[100,5000])
tmp=dfa[dfa.Country.isin(df_now[df_now.Confirmed > 1000]['Country'])]
px.line(tmp,x='Date_updated',y='Confirmed',color='Country',log_y=True, range_y=[100,10000],
range_x=['2020-02-16','2020-03-25'])
EU_country=['Germany','Poland','Romania','Netherlands','Greece','Belgium','Portugal','Czechia','Hungary','Sweden']
tmp=dfa[dfa.Country.isin(EU_country)]
px.line(tmp,x='Date_updated',y='Confirmed',color='Country',log_y=True, range_x=['2020-02-23','2020-03-27'], range_y=[100,5000])
EU_country=['Germany','Poland','Romania','Netherlands','Greece','Belgium','Portugal','Czechia','Hungary','Sweden']
tmp=dfa[dfa.Country.isin(EU_country)]
px.line(tmp,x='StartDate',y='Confirmed',color='Country',log_y=True, range_x=[0,20], range_y=[100,5000])
src="https://data.ontario.ca/dataset/f4112442-bdc8-45d2-be3c-12efae72fb27/resource/455fd63b-603d-4608-8216-7d8647f43350/download/conposcovidloc.csv"
on=pd.read_csv(src)
df=on[['Reported date','Health Unit City','Health Unit Latitude','Health Unit Longitude']]
df.rename(columns={'Reported date':'date','Health Unit City':'City','Health Unit Latitude':'Lat','Health Unit Longitude':'Long'},inplace=True)
df=on[['Reported date','Health Unit City','Health Unit Latitude','Health Unit Longitude']]
df.rename(columns={'Reported date':'date','Health Unit City':'City','Health Unit Latitude':'Lat','Health Unit Longitude':'Long'},inplace=True)
df.City.fillna('Unknown',inplace=True)
df.Lat.fillna(df.Lat.mean(),inplace=True)
df.Long.fillna(df.Long.mean(),inplace=True)
dfc=df.groupby(['date','City','Lat','Long'])['date'].count().reset_index(name='dailycase')
dfc=dfc.sort_values(['City','Lat','Long','date'])
dfc['total_count']=dfc.groupby(['City','Lat','Long']).cumsum()
on['Reported date'].value_counts()
dfc['size'] = dfc['total_count'].pow(0.3) * 2
fig = px.scatter_geo(dfc,
lat="Lat", lon="Long",
color="total_count", size='size', hover_name="City",
range_color=[1,200],
projection="natural earth", animation_frame="date",
title='COVID-19: Cases Over Time', color_continuous_scale="greens"
)
fig.show()
dfa[(dfa.Country=='Canada') & (dfa.Confirmed > 3000) & (dfa.Confirmed < 5000)][['Date_updated','Confirmed','Death']]